package com.xm.xm.undercover.ui

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.widget.Toast
import com.google.gson.Gson
import com.xm.xm.undercover.R
import com.xm.xm.undercover.bean.GetMessage
import com.xm.xm.undercover.bean.RegUser
import com.xm.xm.undercover.service.RegService
import com.xm.xm.undercover.utils.ActivityCollector
import com.xm.xm.undercover.utils.HttpUtils
import com.xm.xm.undercover.utils.MessageUtils
import com.xm.xm.undercover.utils.ServiceCreator
import kotlinx.android.synthetic.main.activity_reg_user.*
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class RegUserActivity : AppCompatActivity(){

    private var name = ""
    private var id = ""
    private var pass = ""
    private var repass = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_reg_user)
        ActivityCollector.addActivity(this)
//        initView()
        bt_reg.setOnClickListener {
            var ans = sendRegInfo(this)
            //清空信息
            clearInfo()
        }
    }

    private fun clearInfo(){
        reg_id.text.clear()
        reg_name.text.clear()
        reg_pass.text.clear()
        reg_repass.text.clear()
    }

    private fun sendRegInfo(activity: Activity){
        name = reg_name.text.toString()
        id = reg_id.text.toString()
        pass = reg_pass.text.toString()
        repass = reg_repass.text.toString()
        if (name.length > 0 && id.length > 0 && pass.length > 0 && repass.length > 0) {
            val regUser = RegUser(id, name, pass, repass)
            val regService = ServiceCreator.create<RegService>()
            val ans = regService.doRegService(regUser)
            val res = ans.enqueue(object:Callback<GetMessage>{
                override fun onFailure(call: Call<GetMessage>, t: Throwable) {
                    t.printStackTrace()
                    Log.d("testActivity","fail")
                }
                override fun onResponse(
                    call: Call<GetMessage>,
                    response: Response<GetMessage>
                ) {
                    val message = response.body()
                    if (message!=null){
                        //分析返回的结果
                        var mesId = MessageUtils.analyzeMes(message,this@RegUserActivity)
                        if (mesId.equals("100")){
                            //返回登录页
                            val intent = Intent(activity,LoginActivity::class.java)
                            startActivity(intent)
                        }
                    }
                }

            })
            Log.d("testActivity","ans.....")

//                }
        } else {
            Toast.makeText(this, "信息不完整", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.removeActivity(this)
    }




}
